<?php
include ("../service/util/authenticateSession.php");
include ("messages.properties.php");  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Sản phẩm</title>
	<?php include("import.php"); ?>
    <script type="text/javascript">
        function handleSearchInput(e,inputF,c){
            var key=e.keyCode || e.which;
            if (key==13){
                window.location.href = "product.php?c="+c+"&keyword="+inputF; 
            }
        }
    </script>
</head>
<body>
<?php
	include("../util/paging.php");
?>
<?php
    include_once("../service/CategoryService.php");
    include_once("../service/ProductService.php");
    $rows_per_page = 9;
    
    //CAT_ID = 0 View all products
    if(isset($_GET["c"])){
        $cat_id = $_GET["c"];
    }else{
        $cat_id = 0;
    }
    
    //Page
    if(isset($_GET["p"])){
        $page = $_GET["p"];
    }else{
        $page = 1;
    }
	
	//Keyword for searching
    if(isset($_GET["keyword"])){
        $keyword = $_GET["keyword"];
    }else{
        $keyword = "";
    }
    
    //Start and End Record
    if($cat_id != 0){
        $numbProduct = countProductOfCategory($cat_id, $keyword);
    }else{
        $numbProduct = countAllProduct($keyword);
    }
    $page_start = ( $page - 1 ) * $rows_per_page;
    $page_end = $page * $rows_per_page;
    
    $products = getProductOfCategory($cat_id, $page_start, $rows_per_page, $keyword);
    
?>
<?php include "header.php"; ?>

<!-- Container -->
<div id="container">
	<div class="shell">
		
		<!-- 
		<div class="small-nav">
			<a href="#">Dashboard</a>
			<span>&gt;</span>
			Current Articles
		</div>
        Small Nav -->
		<!-- End Small Nav -->
		
		<?php
        	$message = "";
			$class = "";
			if (isset($_GET['msg'])) $action = $_GET['msg'];
			
			if(isset($action)){
				if($action == "1") {
					$message = $messages["deleteProductOk"];
					$class = "msg msg-ok";
				}else if($action == "2"){
                    $message = $messages["deleteProductFail"];
					$class = "msg msg-error"; 
				}else if($action == "3"){
                    $message = $messages["deleteProductHasReceipt"];
					$class = "msg msg-error"; 
				}else if($action == "4") {
					$message = $messages["newProductSuccess"];
					$class = "msg msg-ok";
				}
			}
			if ($message == "") $class = 'msg msg-hidden';
		?>
        <!--Message-->
		<div class="<?php echo $class; ?>" id="msg">
            <p><strong><?php echo $message; ?></strong></p>
            <a href="#" onclick="hideMessage();" class="close">close</a>
        </div>
		<br />
		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">
				
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2 class="left">Danh Sách Sản Phẩm</h2>
                        <script>
                            function showProductOfCategory(selectedCat){
                                var catid = selectedCat.options[selectedCat.selectedIndex].value;
                                location.href='product.php?c='+catid; 
                            }
                        </script>
                        <select class="left small-field middle-dropdown" onchange="showProductOfCategory(this);">
                            <option value="0">Tất cả</option>
                            <?php
                            function showAllCategories($cat_par_id, $selected_cat_id, $selected_cat_par_id){
                                $categories = getAllChildrenCategoryOf($cat_par_id);
                                while($aCate = mysql_fetch_array($categories)){
                                    if($aCate["CAT_ID"] != $selected_cat_id){
                                        if($selected_cat_par_id == $aCate["CAT_ID"]){
                                            echo "<option value='".$aCate["CAT_ID"]."' selected='selected'>".$aCate["CAT_NAME_EN"]."-".$aCate["CAT_NAME_VN"]."</option>";
                                        }else{
                                            echo "<option value='".$aCate["CAT_ID"]."'>".$aCate["CAT_NAME_EN"]."-".$aCate["CAT_NAME_VN"]."</option>";
                                        }
                                        
                                        if(hasChild($aCate["CAT_ID"])){
                                            showAllCategories($aCate['CAT_ID'], $selected_cat_id, $selected_cat_par_id);
                                        }
                                    }
                                }
                            }
                            showAllCategories(0, 0, $cat_id)
                            ?>                         
						</select>
						<div class="right">
							<label>Tìm Kiếm:</label>
							<input type="text" class="field small-field" onkeypress="handleSearchInput(event,this.value,<?php echo $cat_id; ?>);"/>
						</div>
					</div>
					<!-- End Box Head -->	

					<!-- Table -->
					<div class="table">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
                                <th>STT</th>
								<th>Tên Sản Phẩm (Tiếng Anh)</th>
                                <th>Tên Sản Phẩm (Tiếng Việt)</th>
                                <th>Giá tiền</th>
								<th>Ngày tạo</th>
								<th>Phân Loại</th>
								<th width="110" class="ac">Thao Tác</th>
							</tr>
                            <?php
                                $stt = 1;
                                if(mysql_num_rows($products) > 0){
                                    while($aProduct = mysql_fetch_array($products)){
                            ?>
                                <tr>
                                    <td><?php echo $stt; ?></td>
    								<td><h3><a href="product-detail.php?do=view&n=<?php echo $aProduct["PRO_ID"]; ?>"><?php echo $aProduct["PRO_NAME_EN"]; ?></a></h3></td>
                                    <td><h3><a href="product-detail.php?do=view&n=<?php echo $aProduct["PRO_ID"]; ?>"><?php echo $aProduct["PRO_NAME_VN"]; ?></a></h3></td>
                                    <td><?php echo round($aProduct["PRO_PRICE"]); ?></td>
    								<td>
                                    <?php 
                                        $ts = strtotime($aProduct["PRO_ADDEDDATE"]);
                                        echo date("d/m/Y", $ts);
                                    ?>
                                    </td>
    								<td>
                                    <?php
                                        $cate = mysql_fetch_array(getCategory($aProduct["CAT_ID"]));
                                    ?>
                                    <a href="category-detail.php?do=view&n=<?php echo $cate["CAT_ID"]; ?>">
                                        <?php echo $cate["CAT_NAME_EN"]."-".$cate["CAT_NAME_VN"]; ?>
                                    </a>
                                    </td>
    								<td>
                                    <?php
                                    if(!hasReceipt($aProduct["PRO_ID"])){
                                        echo "<a href='../action/DeleteProductAction.php?n=".$aProduct["PRO_ID"]."' class='ico del'>Delete</a>";                                
                                    }else{
                                        echo "<a href='#' onclick='alert(\"Sản phẩm không thể xóa vì có hóa đơn\")' class='ico del'>Delete</a>";    
                                    }
                                    ?>
                                    
                                    </td>
    							</tr>
                            <?php
                                    $stt++;
                                    }        
                                    
                                }
                            ?>
							
						</table>
						
						
						<!-- Pagging -->
						<div class="pagging">
							<div class="left">
                                <?php
                                if($numbProduct > 0){
                                    if($page_end <= $numbProduct){
                                        echo "Showing ".($page_start+1)."-".($page_end)." of ".$numbProduct;    
                                    }else{
                                        echo "Showing ".($page_start+1)."-".($numbProduct)." of ".$numbProduct;
                                    }    
                                }
                                ?>
                            </div>

							<div class="right">
								<?php echo paging($page, ceil($numbProduct/$rows_per_page), "", "product.php?c=".$cat_id."&keyword=".$keyword."&p="); ?>
							</div>

						</div>
						<!-- End Pagging -->
						
					</div>
					<!-- Table -->
					
				</div>
				<!-- End Box -->
				
                <!-- Box -->
                <div class="box">
                	<!-- Box Head -->
                    <div class="box-head">
                    	<h2>Thao tác</h2>
                    </div>
                    <!-- End ofBox Head -->
                    <div class="box-content">
                    	<div class="buttons">
                            <input type="button" class="button" value="Thêm Sản Phẩm" onClick="window.location='product-detail.php?do=add'" /> &nbsp;
                            <!--<input type="button" class="button" value="Xóa Sản Phẩm"/>--> 
                        </div>
                    </div>
                </div>
			</div>
			<!-- End Content -->
			

			
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->

<?php
	include "footer.php";
	ConnectionManager::getInstance()->closeConnection();
?>	
	
</body>
</html>